package com.hs.homework_02.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hs.homework_02.entitiy.TbStudent;
import com.hs.homework_02.service.TbStudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @ClassName PageControlller
 * @Description TODO
 */
@Api(tags = "页面路由接口",description = "处理页面的跳转和请i求页面")
@Controller
public class PageControlller {
    @Autowired
    TbStudentService service;

    @ApiOperation(tags = "前往list页面并携带列表的数据",value = "前往list页面并携带列表的数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "分页查询的页码")
    })
    @GetMapping("/list")
    public String toList(HttpSession session, Model model,@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum){
        String path = isLogin(session,"list");
        if("list".equals(path)) {
            Page<TbStudent> page =new Page<>(pageNum,5);
            Page<TbStudent> studentPage = service.page(page);
            model.addAttribute("studentList",studentPage.getRecords());
            model.addAttribute("total",studentPage.getTotal());
            model.addAttribute("pageInfo",studentPage);
        }
        return path;
    }

    @GetMapping("/update/{id}")
    public String toUpdate(HttpSession session, Model model,@PathVariable Integer id){
        String path = isLogin(session,"update");
        if("update".equals(path)){
            model.addAttribute("student",service.getById(id));
        }
        return path;
    }

    @GetMapping("/remove/{id}")
    public String remove(@PathVariable Integer id){
        service.removeById(id);
        return "redirect:/list";
    }

    @GetMapping("/add")
    public String toAdd(HttpSession session, Model model){
        return isLogin(session,"add");
    }

    private String isLogin(HttpSession session,String path){
        if(session.getAttribute("user") == null){
            return "login";
        }
        return path;
    }

    @GetMapping("/login")
    public String toLogin(){
        return "index";
    }

    @GetMapping("/uploadFile")
    public String toUploadFile(){
        return "uploadFile";
    }

    @PostMapping("/condition")
    public String condition(TbStudent student, Model model){
        LambdaQueryWrapper<TbStudent> wrapper = new LambdaQueryWrapper<>();
        wrapper.func(i -> {
                    if(student.getName()!=null && !"".equals(student.getName())) {
                        i.like(TbStudent::getName,student.getName());
                    }
        }).func(i -> {
            if(student.getSex()!=null && !"".equals(student.getSex())) {
                i.eq(TbStudent::getSex,student.getSex());
            }
        }).func(i -> {
            if(student.getAddress()!=null && !"".equals(student.getAddress())){
                i.eq(TbStudent::getAddress,student.getAddress());
            }
        });
        Page<TbStudent> page = service.page(new Page<TbStudent>(1, 5), wrapper);
        model.addAttribute("studentList",page.getRecords());
        model.addAttribute("total",page.getTotal());
        model.addAttribute("pageInfo",page);
        return "list";
    }
}
